/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package izsla.entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Describes a trip.
 */
@Entity
@NamedQueries({
@NamedQuery(
    name="Trip.getTrips",
    query="SELECT t FROM Trip t"
    + " WHERE "
        + " (t.distance BETWEEN :minDist AND :maxDist) AND"
        + " :start <= t.startTime AND t.endTime <= :end"
)
})
public class Trip implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private TripUser organiser;

	/** The link to the Google map of the trip.*/
    @Column(length=1500)
    private String mapURL;

    private String description;

    private int distance;

    private int limit;

    @Temporal(TemporalType.DATE)
    private Date startTime;

    @Temporal(TemporalType.DATE)
    private Date endTime;


    @OneToMany(mappedBy="trip")
    private Collection<Image> images;

    @OneToMany(mappedBy="trip")
    private Collection<News> newsItem;

    @OneToMany(mappedBy="trip", cascade = CascadeType.PERSIST)
    private Collection<LodgingType> lodgingTypes;

    @OneToMany(mappedBy="trip")
    private Collection<Participate> participants;

    public Long getId() {
		return id;
    }

    public void setId(Long id) {
		this.id = id;
    }

	/** Get the description of the trip. */
    public String getDescription() {
		return description;
    }

    public void setDescription(String description) {
	this.description = description;
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public Date getEndTime() {
	return endTime;
    }

    public void setEndTime(Date endTime) {
	this.endTime = endTime;
    }

    public String getMapURL() {
	return mapURL;
    }

    public void setMapURL(String mapURL) {
	this.mapURL = mapURL;
    }

    public TripUser getOrganiser() {
	return organiser;
    }

    public void setOrganiser(TripUser organiser) {
	this.organiser = organiser;
    }

    public Date getStartTime() {
	return startTime;
    }

    public void setStartTime(Date startTime) {
	this.startTime = startTime;
    }

    public Collection<Image> getImages() {
	return images;
    }

    public Collection<News> getNewsItem() {
	return newsItem;
    }

    public Collection<LodgingType> getLodgingTypes() {
	return lodgingTypes;
    }

    public void setLodgingTypes(Collection<LodgingType> lodgingTypes) {
	this.lodgingTypes = lodgingTypes;
    }

    @Override
    public String toString() {
	return "izsla.entities.Trip[id=" + id + "]";
    }

    public void setImages(Collection<Image> images) {
	this.images = images;
    }

    public void setNewsItem(Collection<News> newsItem) {
	this.newsItem = newsItem;
    }

    public int getNumParticipants() {
	return participants.size();
    }

    public Collection<Participate> getParticipants() {
	return participants;
    }

    public void setParticipants(Collection<Participate> participants) {
	this.participants = participants;
    }

    @Override
    public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final Trip other = (Trip) obj;
		if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
			return false;
		}
		return true;
    }

    @Override
    public int hashCode() {
		int hash = (this.id != null ? this.id.hashCode() : 0);
		return hash;
    }
}
